package zj.com.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.zfdang.multiple_images_selector.ImagesSelectorActivity;
import com.zfdang.multiple_images_selector.SelectorSettings;

import java.io.File;
import java.util.List;

import zj.com.activities.JcdDetailEditActivity;
import zj.com.net.Urls;
import zj.com.util.ImageUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
public class LookImgGridAdapter extends BaseAdapter{

    public List<String> list;
    public Activity context;
    public LayoutInflater inflater;

    public boolean isEdit = false;
    private DisplayImageOptions options;
    public LookImgGridAdapter(List<String> list, Activity context){

        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
        options = ImageUtil.getOption();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder ;
        if (convertView == null) {
            viewHolder= new ViewHolder();
            convertView = inflater.inflate(R.layout.adapter_upload_img_grid, null);
            viewHolder.img_iv = (ImageView) convertView.findViewById(R.id.img_iv);
            viewHolder.pb = (ProgressBar) convertView.findViewById(R.id.pb);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String url = list.get(position);
        String index = url.replace(Urls.GetResourceContent, "");


        LogUtils.getInstance().error("=====index=="+index);
        String newUrl = String.format(Urls.GetImg, index, "Min");
        LogUtils.getInstance().error("====newUrl===" + newUrl);

      // viewHolder.img_iv.setImageURI(Uri.parse("http://202.127.25.114:8081/api/sqcp/resource/GetImage?id=278&Type=Min"));

        ImageLoader.getInstance()
                .displayImage(newUrl, viewHolder.img_iv, options, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {

                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                        viewHolder.pb.setVisibility(View.GONE);
                    }

                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        viewHolder.pb.setVisibility(View.GONE);

                    }
                }, new ImageLoadingProgressListener() {
                    @Override
                    public void onProgressUpdate(String imageUri, View view, int current, int total) {
                    }
                });

        return convertView;
    }
    private static class ViewHolder {
        ImageView img_iv;
        ProgressBar pb;

    }

    public void setDataChange(List<String> list){


        this.list = list;
        notifyDataSetChanged();
    }

}
